module.exports = class extends think.kms.admin {
  constructor(ctx) {
    super(ctx);
    this.db = this.model('kms/member');
    this.tactive = 'user';
  }

  async indexAction() {
    const map = {
      'status': ['>', -1]
    };
    if (!think.isEmpty(this.get('username'))) {
      map.username = ['like', '%' + this.get('username') + '%'];
    }
    if (!think.isEmpty(this.get('mobile'))) {
      map.mobile = ['like', '%' + this.get('mobile') + '%'];
    }
    if (!think.isEmpty(this.get('email'))) {
      map.email = ['like', '%' + this.get('email') + '%'];
    }
    if (!think.isEmpty(this.get('date'))) {
      map.last_login_time = ['like', '%' + this.get('date') + '%'];
    }
    const data = await this.db.where(map).page(this.get('page') || 1, 20).order('id ASC').countSelect();
    const html = this.pagination_admin(data);
    this.assign('pagerData', html);
    this.assign('list', data.data);
    this.assign({
      title: '系统用户管理'
    })
    await this.display();
  }

  async passAction() {
    if (this.isPost) {
      const password = this.post().password;
      if (password !== this.post().repassword) {
        return this.fail('两次输入的密码不一致！');
      }
      const result = await this.db.where({id: this.get().id}).update({'password': encryptPassword(password)})
      if (result) {
        return this.success('密码修改成功，下次请使用新密码登录！');
      }
      return this.fail('密码修改失败，请稍候再试！');
    }
    if (this.isGet) {
      const userinfo = await this.db.where({id: this.get().id}).find();
      this.assign({
        verify: false,
        user: userinfo
      });
      await this.display('admin/user_pass');
    }
  }

  async addAction() {
    if (this.isGet) {
      const authorizes = await this.model('auth_role').order('id ASC').select();
      this.assign({
        authorizes
      })
      return this.display('admin/user_form');
    } else if (this.isPost) {
      return this.display('admin/user_form');
    }
  }

  async editAction() {
    if (this.isGet) {
      const user = await this.db.where({id: this.get().id}).find();
      const authorizes = await this.model('auth_role').order('id ASC').select();
      this.assign({
        user,
        authorizes
      })
      return this.display('admin/user_form');
    } else {
      const params = this.post();
      const password = this.post().password;
      if (password !== this.post().repassword) {
        return this.fail('两次输入的密码不一致！');
      }
      const map = {};
      map.authorize = this.post('authorize');
      console.log(map.authorize);
      if (think.isArray(map.authorize)) {
        map.authorize = map.authorize.sort(function(a, b) { return a - b }).join(',');
      }
      map.email = this.post('email');
      map.id = this.post('id');
      map.mobile = this.post('mobile');
      const id = this.post('id');
      if (id) {
        const result = await this.db.where({id}).update(map);
        return this.success('修改成功');
      } else {
        map.username = this.post('username');
        map.password = encryptPassword(password)
        map.status = 1;
        map.is_admin = this.post('is_admin');
        map.reg_time = new Date().getTime();
        const ret = await this.db.where({username: map.username}).find();
        console.log(ret)
        if (!think.isEmpty(ret)) {
          return this.fail('用户名已存在');
        }
        const self = this;
        let result;
        if (map.is_admin == 1) {
          result = await this.db.transaction(async() => {
            const userId = await self.db.add(map);
            return await self.model('auth_user_role').db(self.db.db()).add({
              user_id: userId,
              role_id: map.authorize
            });
          });
        } else {
          result = await this.db.add(map);
        }
        console.log(map);
        if (result) {
          return this.success('添加成功！');
        } else {
          return this.fail('添加失败!');
        }
      }
    }
  }
  
  async forbidAction() {
    const ids = this.post().id.split(',');
    const status = this.post().value;
    let result = await this.db.where({id: ids}).update({status});
    if (result) {
      return this.success('操作成功！');
    } else {
      return this.fail('操作失败');
    }
  }
  
  async resumeAction() {
    const ids = this.post().id.split(',');
    const status = this.post().value;
    let result = await this.db.where({id: ids}).update({status});
    if (result) {
      return this.success('操作成功！');
    } else {
      return this.fail('操作失败');
    }
  }

  async delAction() {
    console.log(this.post())
    const ids = this.post().id.split(',');
    let result = await this.db.where({id: ids}).update({status: -1});
    if (result) {
      return this.success('删除成功！');
    } else {
      return this.fail('删除失败');
    }
  }
}